package santiago.view;

import java.util.regex.Matcher;
import java.util.regex.Pattern;
import santiago.client.Client;
import santiago.exception.EmptyFieldException;
import santiago.exception.IPAddressPatternException;

public class SantiagoValidation {

    public static boolean checkIP(String IP) {
        boolean validate = true;
        final String IPADDRESS_PATTERN =
                "^([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\."
                + "([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\."
                + "([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\."
                + "([01]?\\d\\d?|2[0-4]\\d|25[0-5])$";
        Pattern IPPattern = Pattern.compile(IPADDRESS_PATTERN);
        Matcher Matcher = IPPattern.matcher(IP);
        if (!Matcher.matches()) {
            validate = false;
            Client.getInterface().handleException(new IPAddressPatternException());
        }
        return validate;
    }

    public static boolean checkEmptyField(String Data) {
        boolean validate = true;
        if (Data.isEmpty()) {
            Client.getInterface().handleException(new EmptyFieldException());
            validate = false;
        }
        return validate;
    }

    public static boolean checkIsInteger(String Data) {
        try {
            Integer.parseInt(Data);
        } catch (NumberFormatException e) {
            Client.getInterface().handleException(e);
            return false;
        }
        return true;
    }
}
